package edu.codink.common.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import edu.codink.xq.center.entity.Userinfo;

/**
 * 公共的CONTROLLER类
 * 
 * @author ylsu
 * 
 */
public abstract class BaseController
{
	/**
	 * SESSION中存储登录用户的KEY值
	 */
	private final static String LOGIN_USER = "loginUser";
	/**
	 * 当前的页码参数
	 */
	private final static String CURRENT_PAGE = "currentPage";
	/**
	 * 当前各页数量的参数
	 */
	private final static String PAGE_SIZE = "pageSize";
	/**
	 * 由控制器向前台传递的分页参数名称
	 */
	protected final static String PARAM_PAGE = "page";
	/**
	 * 由控制器向前台传递的列表参数名称
	 */
	protected final static String PARAM_LIST = "list";
	/**
	 * 用户动作处理后JSON返回的成功标志位
	 */
	protected final static String PARAM_SUCCESS = "1";
	/**
	 * 用户动作处理后JSON返回的失败标志位
	 */
	protected final static String PARAM_FAILED = "0";
	/**
	 * 用于模态窗口执行完毕后返回至成功的页面
	 */
	protected final static String SUCCESS_PAGE = "success";
	
	/**
	 * 创建VIEW
	 * @return
	 */
	protected SpringModelView buildView()
	{
		return new SpringModelView();
	}
	
	/**
	 * 根据REQUEST获取当前登录的用户信息
	 * @param session
	 * @return
	 */
	protected Userinfo getLoginUser(HttpServletRequest request)
	{
		return (Userinfo) request.getSession().getAttribute(LOGIN_USER);
	}
	
	/**
	 * 获取分类的类信息
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	protected Page getPage(HttpServletRequest request)
	{
		Page page = new Page();
		if(request.getParameter(CURRENT_PAGE) != null)
		{
			page.setCurrentPage(Long.parseLong(request.getParameter(CURRENT_PAGE).toString()));
		}
		if(request.getParameter(PAGE_SIZE) != null)
		{
			page.setPageSize(Long.parseLong(request.getParameter(PAGE_SIZE).toString()));
		}
		return page;
	}
	
	/**
	 * 得到page
	 * @param start
	 * @param limit
	 * @param count
	 * @return
	 */
	public Page getPage(HttpServletRequest request,long count)
	{
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		Page page = new Page();
		if(start == 0)
		{
			page.setCurrentPage(1);
		}
		else
		{
			page.setCurrentPage((start / limit) + 1);
		}
		page.setPageSize(limit);
		if(count > 0)
		{
			page.setRecordCount(count);
		}
		page.setPageCount((start / limit) + 1);
		return page;
	}
	
	/**
	 * 根据REQUEST获取当前登录的用户信息
	 * @param session
	 * @return
	 */
	protected void setLoginUser(HttpServletRequest request,Userinfo user)
	{	
		request.getSession().setAttribute(LOGIN_USER,user);
	}
	/**
	 * 将结果转化为JSON
	 * @param obj
	 * @param response
	 */
	public void outJson(Object obj,HttpServletResponse response)
	{
		response.setContentType("text/html;charset=UTF-8");
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		String jsonStr = gson.toJson(obj);
		PrintWriter out = null;
		try
		{
			out = response.getWriter();
			out.write(jsonStr);
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(out != null)
			{
				out.close();
			}
		}
	}
}
